International pricing

In Afosto, you have the flexibility to define different price lists for different countries. To implement these price lists for a specific country in the context of a cart, we can set a country code on the cart.

Setting a country code can be achieved using the Afosto client or a GraphQL mutation.

Using Afosto client

Here's how you can set a country code using the Afosto JavaScript client:

1const cart = await client.setCountryCodeOnCart('FR');

In this example, we're setting the country code to 'FR' for France. You can replace 'FR' with the appropriate country code.

Using GraphQL

Alternatively, you can set a country code on the cart using a GraphQL mutation. The setCountryCodeOnCart mutation allows you to specify a country code for a given cart.

1import { gql } from '@afosto/storefront';
2import StorefrontClient, { CoreCartFragment } from '@afosto/storefront';
3
4const client = StorefrontClient({
5  storefrontToken: 'STOREFRONT_TOKEN',
6});
7
8const query = gql`
9  ${CoreCartFragment}
10  mutation SetCountryCodeOnCart($set_country_code_on_cart_input: SetCountryCodeOnCartInput!) {
11    setCountryCodeOnCart(input: $set_country_code_on_cart_input) {
12      cart {
13        ...CoreCartFragment
14      }
15    }
16  }
17`;
18
19const variables = {
20  setCountryCodeOnCartInput: {
21		cartId: client.getCartTokenFromStorage(),
22    countryCode: 'FR',
23  },
24};
25
26const response = await client.query(query, variables);

Here we're using the SetCountryCodeOnCart mutation, passing in the cart ID and the country code. Note that we're using 'FR' as the country code in this example. You should replace 'FR' with the desired country code.

This GraphQL mutation will return a cart object (with the applied country code), which can then be used as needed within your application.